package evs1.restframework.util;

import java.lang.annotation.Annotation; 
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import evs1.restframework.anno.Evs11Rest;
import evs1.restframework.anno.Evs11Search;

public class AnnotatedClassContainer 
{
	// url defined for the class
	private String definedUrl;
	private Class<?> theClass;
	private Field[] searchableFields;

	public AnnotatedClassContainer(Class<?> theClass) 
	{
		this.theClass = theClass;
		// set uri as defined in annotation
		this.definedUrl = theClass.getAnnotation(Evs11Rest.class).plural();
		
		// load fields that can be searched
		// -> check, if some attributes are annotated with @Evs11Search
		Field[] fields = theClass.getDeclaredFields();
		List<Field> searchableFieldsAsList = new ArrayList<Field>();
		for(int i = 0; i < fields.length; i++)
		{
			Annotation[] an = fields[i].getAnnotations();
			for(int j = 0; j < an.length; j++)
			{
				if(an[j].annotationType().equals(Evs11Search.class))
					searchableFieldsAsList.add(fields[i]);
			}
		}
		searchableFields = new Field[searchableFieldsAsList.size()];
		searchableFieldsAsList.toArray(searchableFields);
	}

	public String getDefinedUrl() {
		return definedUrl;
	}

	public void setDefinedUrl(String definedUrl) {
		this.definedUrl = definedUrl;
	}

	public Class<?> getTheClass() {
		return theClass;
	}

	public void setTheClass(Class<?> theClass) {
		this.theClass = theClass;
	}

	public Field[] getSearchableFields() {
		return searchableFields;
	}

	public void setSearchableFields(Field[] searchableFields) {
		this.searchableFields = searchableFields;
	}
}